<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/5/23
 * Time: 16:19
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Component\RecordComponent;
use Home\Exception\HomeException;
use Home\Request\record\AddRecordRequest;
use Home\Response\AddRecordResponse;

class AddRecordService implements Service
{
    public function run(AddRecordRequest $request = null)
    {
        if ($request->issetActId() == false)
            throw new HomeException(10001);
        if ($request->verifyActId() == false)
            throw new HomeException(10002);
        if ($request->IsActIdInRange() == false)
            throw new HomeException(10003);

        if ($request->issetApplyId() == false)
            throw new HomeException(10001);
        if ($request->verifyApplyId() == false)
            throw new HomeException(10002);
        if ($request->IsApplyIdInRange() == false)
            throw new HomeException(10003);

        if ($request->issetUserId() == false)
            throw new HomeException(10001);
        if ($request->verifyUserId() == false)
            throw new HomeException(10002);
        if ($request->IsUserIdInRange() == false)
            throw new HomeException(10003);

        if ($request->issetTime() == false)
            throw new HomeException(10001);
        /*  if ($request->verifyTime() == false)
              throw new HomeException(10002);*/
        if ($request->IsTimeInRange() == false)
            throw new HomeException(10003);


        $act_id = $request->getActId();
        $apply_id = $request->getApplyId();
        $user_id = $request->getUserId();
        $time = $request->getTime();
        $nickname = $request->getNickName();
        $headimgurl = $request->getHeadImgUrl();

        $data = array(
            'act_id' => $act_id,
            'apply_id' => $apply_id,
            'user_id' => $user_id,
            'time' => $time,
            'nickname' => $nickname,
            'headimgurl' => $headimgurl,
        );

        $component = new RecordComponent();
        $response = new AddRecordResponse();
        try {
            $data = $component->AddRecord($data);
            if (is_null($data) == true) {
                $response->setItIsNull();
            }
        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}